package com.sl.hx.shop.utils;

import android.text.TextUtils;
import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author wyq
 * @date 2018/12/1
 */

public class TimeUtil {


    /**
     * 将秒转化为 分钟：秒 的格式
     *
     * @param millisecond 秒
     * @return
     */
    public static String formatTime(long millisecond) {
        int hour; //小时
        int minute;//分钟
        int second;//秒数
        hour = (int)(millisecond/60/60);
        minute = (int) (millisecond / 60);
        second = (int) (millisecond  % 60);
        if (minute < 10) {
            if (second < 10) {
                return hour+ ":" + "0" + minute + ":" + "0" + second;
            } else {
                return hour+ ":" + "0" + minute + ":" + second;
            }
        }else {
            if (second < 10) {
                return hour+ ":" + minute + ":" + "0" + second;
            } else {
                return hour+ ":" + minute + ":" + second;
            }
        }
    }

    /**
     * 根据毫秒时间戳来格式化字符串
     * 今天显示今天、昨天显示昨天、前天显示前天.
     * 早于前天的显示具体年-月-日，如2017-06-12；
     * @param timeStamp 毫秒值
     * @return 今天 昨天 前天 或者 yyyy-MM-dd HH:mm:ss类型字符串
     */
    public static String getDateToString(long timeStamp) {
        long curTimeMillis = System.currentTimeMillis();
        Date curDate = new Date(curTimeMillis);
        int todayHoursSeconds = curDate.getHours() * 60 * 60;
        int todayMinutesSeconds = curDate.getMinutes() * 60;
        int todaySeconds = curDate.getSeconds();
        int todayMillis = (todayHoursSeconds + todayMinutesSeconds + todaySeconds) * 1000;
        long todayStartMillis = curTimeMillis - todayMillis;
        if(timeStamp >= todayStartMillis) {
            return "今天";
        }
        int oneDayMillis = 24 * 60 * 60 * 1000;
        long yesterdayStartMilis = todayStartMillis - oneDayMillis;
        if(timeStamp >= yesterdayStartMilis) {
            return "昨天";
        }
        long yesterdayBeforeStartMilis = yesterdayStartMilis - oneDayMillis;
        if(timeStamp >= yesterdayBeforeStartMilis) {
            return "前天";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
       // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return  sdf.format(new Date(timeStamp));
    }


    /**
     * 日期格式为yyyy-MM-dd HH:mm:ss
     *
     * @param str1 the first date
     * @param str2 the second date
     * @return true <br/>false
     */
    public static String isDateOneBigger(String str1, String str2) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dt1 = null;
        Date dt2 = null;
        try {
            dt1 = sdf.parse(str1);
            dt2 = sdf.parse(str2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if ((dt1.getTime() - dt2.getTime())/(1000*60)<120&&(dt1.getTime() - dt2.getTime())/(1000*60)>=0) {
            //Log.e("gettime","疯抢中"+(dt1.getTime() - dt2.getTime())/(1000*60));
            return "疯抢中";
        } else if ((dt1.getTime() - dt2.getTime())/(1000*60)<0) {
            //Log.e("gettime","即将开始"+(dt1.getTime() - dt2.getTime())/(1000*60));
            return "即将开始";
        }else {
            //Log.e("gettime","已开抢"+(dt1.getTime() - dt2.getTime())/(1000*60));
            return "已开抢";
        }

    }

    public static String dataFormat(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
        String time = format.format(date) ;
        return time ;
    }

    /**
     * 根据月份获取相应的英文名称
     * @param month
     * @return
     */
    public static String dateToEnglish(int month){
        String[] monthEnglish = {"January", "February", "March", "April", "May", "June", "July",
                                "August", "September", "October", "November", "December"} ;
        return monthEnglish[month-1] ;
    }

    /**
     * 判断结束日期是否大于开始日期
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean isDateGreater(Date startTime, Date endTime){

        boolean isGreater = false ;

        Calendar mStartTime = Calendar.getInstance() ;
        mStartTime.setTime(startTime);
        int startYear = mStartTime.get(Calendar.YEAR) ;
        int startMonth = mStartTime.get(Calendar.MONTH) ;
        int startDay = mStartTime.get(Calendar.DAY_OF_MONTH) ;

        Calendar mEndTime = Calendar.getInstance() ;
        mEndTime.setTime(endTime);
        int endYear = mEndTime.get(Calendar.YEAR);
        int endMonth = mEndTime.get(Calendar.MONTH) ;
        int endDay = mEndTime.get(Calendar.DAY_OF_MONTH) ;

        if (startYear > endYear){
            isGreater =  true ;
        }else if (startYear == endYear){
            if (startMonth > endMonth){
                isGreater =  true ;
            }else if (startMonth == endMonth){
                if (startDay > endDay){
                    isGreater =  true ;
                }else{
                    isGreater =  false ;
                }
            }
        }else{
            isGreater =  false ;
        }
        return isGreater ;
    }

    /**
     * 根据出生日期获取年龄
     * @param date
     * @return
     */
    public static int getAge(Date date){
        //年龄
        int age = 0 ;
        //当前日期
        Calendar currentDate = Calendar.getInstance();
        //出生日期
        Calendar dateOfBirth = Calendar.getInstance();
        dateOfBirth.setTime(date);

        if (dateOfBirth.after(currentDate)){
            age = 0;
        }else{
            //计算周岁
            age = currentDate.get(Calendar.YEAR) - dateOfBirth.get(Calendar.YEAR) - 1 ;
            if (currentDate.get(Calendar.MONTH) > dateOfBirth.get(Calendar.MONTH)){
                age ++ ;
            }
        }
        return age ;
    }

    /**
     * 根据出生日期判断星座
     * @param date
     * @return
     */
    public static String getConstellations(Date date){
        //星座
        String constellations = "" ;
        //出生日期
        Calendar dateOfBirth = Calendar.getInstance();
        dateOfBirth.setTime(date);

        String[] starArr = {"魔羯座","水瓶座", "双鱼座", "白羊座",
                "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座" };
        int[] dayArr = {19, 18, 20, 19, 20, 21, 22, 22, 22, 23, 22, 21};  // 两个星座分割日
        int index = dateOfBirth.get(Calendar.MONTH) ;

        /**
         * 根据出生的日期和当个月的星座的日期分隔日做比较，如果大于当月星座的日期，否则为下个星座
         */
        if (dateOfBirth.get(Calendar.DAY_OF_MONTH) > dayArr[dateOfBirth.get(Calendar.MONTH)]){
            if (index == dayArr.length - 1){
                index = 0 ;
            }else{
                index++ ;
            }
        }

        constellations = starArr[index] ;

        return constellations ;
    }

    /**
     * 将long类型的时间转化为年月日格式
     * @param time
     * @return
     */
    public static String timeFormat(long time){
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(time) ;
        String str = sdf.format(date) ;
        return str ;
    }

    /**
     * 将long类型的时间转化为时分秒
     * @param time
     * @return
     */
    public static String timeFormatToHMS(long time){
        Date date = new Date(time) ;
        DateFormat df = DateFormat.getTimeInstance() ;
        String surplusTimie = df.format(date) ;
        return surplusTimie ;

    }

    /**
     * 将long类型的时间转换成年月日时分
     * @param time
     * @Y
     */
    public static String timeFormatToYMDHM(long time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm") ;
        Date date = new Date(time) ;
        String str = sdf.format(date) ;
        return str ;
    }

    /**
     * 将long类型的时间转换成年月日时分秒
     * @param time
     * @return
     */
    public static String timeFormatToYMDHMS(long time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
        Date date = new Date(time) ;
        String str = sdf.format(date) ;
        return str ;
    }

    /**
     * 将long类型的时间转换成月日时分
     * @param time
     * @return
     */
    public static String timeFormatToMDHM(long time){
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm") ;
        Date date = new Date(time) ;
        String str = sdf.format(date) ;
        return str ;
    }

    /**
     * 将data类型的时间转换成月-日
     * @param date
     * @return
     */
    public static String timeFormatToMD(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") ;
        return sdf.format(date) ;
    }

    /**
     * 获取系统时间戳
     * @return
     */
    public long getCurTimeLong(){
        long time=System.currentTimeMillis();
        return time;
    }
    /**
     * 获取当前时间
     * @param pattern
     * @return
     */
    public static String getCurDate(String pattern){
        SimpleDateFormat sDateFormat = new SimpleDateFormat(pattern);
        return sDateFormat.format(new java.util.Date());
    }

    /**
     * 时间戳转换成字符窜
     * @param milSecond
     * @param pattern
     * @return
     */
    public static String getDateToString(long milSecond, String pattern) {
        Date date = new Date(milSecond);
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 将字符串转为时间戳
     * @param dateString
     * @param
     * @return
     */
    public static long getStringToDate(String dateString) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try{
            date = dateFormat.parse(dateString);
        } catch(ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }




}
